package com.sunshine.androidjavapractise.ui.activity;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sunshine.androidjavapractise.R;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBack;
import com.zhouyou.http.exception.ApiException;

import java.io.File;

public class FolderActivity extends AppCompatActivity {
    private final String URL = "/home/GetNotice?listId=demo4444";
    private Button mButton;
    private EditText mEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_folder);
        initView();
    }

    private void initView() {
        mButton = findViewById(R.id.btn_folder);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyHttp.post(URL).baseUrl("http://192.168.9.200:9908").execute(new CallBack<String>() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(ApiException e) {
                        System.out.println("ApiException s = " + e.getMessage());
                    }

                    @Override
                    public void onSuccess(String s) {
                        System.out.println("onSuccess s = " + s);
                    }
                });
                String text = mEditText.getText().toString();
                if (!TextUtils.isEmpty(text)) {
                    makeFolder(text);
                }

            }
        });
        mEditText = findViewById(R.id.edt_text);
    }

    private void makeFolder(String folderName) {
        String parent = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
        File folder = new File(parent + folderName);
        if (!(folder.exists() && folder.isDirectory())) {
            folder.mkdirs();
        }
    }

    private void showDialog(String text) {

    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    }
}
